import { _decorator, Label, UIOpacity, UITransform } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import type { Popup } from '../page/pop-up';
const { ccclass, property } = _decorator;

@ccclass('ppWait')
export class ppWait extends BasePopUp<typeof Popup, null> {
	protected default_return: null;

	private ui_container: UITransform;
	private uiop_list: Array<UIOpacity>;

	private angle_step: number;
	private running: boolean;

	protected update(dt: number){
		if(this.running){
			this.setAngle(this.ui_container.node.angle + this.angle_step * dt);
		}
	}

	protected initAfterOnLoad(){
		this.init();
		this.initUIStatus();
	}

	public onShow(prms: Promise<any>){
		this.running = true;
		prms.then(()=>{
			this.running = false;
			this.setAngle(0);
			this.close(null);
		});
	}

	private init(){
		this.ui_container = this.node.getChildByName('container').getComponent(UITransform);
		this.uiop_list = this.ui_container.node.children.map(node=>node.getComponent(UIOpacity));
		this.angle_step = -(360 / 2.5);
	}

	private initUIStatus(){
		this.node.getChildByName('label').getComponent(Label).string = 'Please wait, payment in progress.';
	}

	private setAngle(angle: number){
		this.ui_container.node.angle = angle % 360;
		angle = this.ui_container.node.angle;

		this.uiop_list.forEach(uiop=>{
			let scale = -(angle + uiop.node.angle - 180) % 360 / 120;
			if(scale > 1){
				scale = scale > 2 ? 3-scale : 1;
			}

			uiop.opacity = 255 * scale;
			scale = 0.5 + 0.5 * scale;
			uiop.node.scale.set(scale, scale, 1);
		});
	}
}

